package com.example.accounting.slice;

import com.example.accounting.ResourceTable;
import com.example.accounting.provider.AccountItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.util.ArrayList;
import java.util.HashMap;

public class MainAbilitySlice extends AbilitySlice {
    private final int buttonSize = 60;
    private int days = 10;

    private DirectionalLayout buttonLayout;
    private String[] buttonTexts = {"资产", "统计", "添加", "导入"};

    private Button[] buttons;
    private DirectionalLayout[] details;

    private ListContainer listContainer;
    private Text outcome;
    private Text income;

    private AccountItemProvider contactItemProvider;

    private static final int DIALOG_WIDTH = 900;

    private static final int DIALOG_HEIGHT = 900;

    private static final int DIALOG_CORNER_RADIUS = 30;

    private Text show_year;
    private Text show_month;

    private HashMap<String, Double> hashMap;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initComponents();
        initProvider();
        updateIncomeOutcome();

        if ((intent.getStringParam("intent") != null) && (intent.getStringParam("intent").equals("insert"))) {
            System.out.println("want to insert");
            boolean a = contactItemProvider.insert(intent.getStringParam("date"), intent.getDoubleParam("money", 0), intent.getStringParam("type"), intent.getStringParam("info"), intent.getStringParam("account"));
            contactItemProvider.update(show_year.getText(), show_month.getText());
            System.out.println("insert a account, result is " + a);
            updateIncomeOutcome();
        }
    }

    public void updateIncomeOutcome() {
        // 更新总支出、收入
        hashMap = contactItemProvider.getInOutCome();
        income.setText(String.valueOf(hashMap.get("income")));
        outcome.setText(String.valueOf(hashMap.get("outcome")));
    }

    private void initButtons() {
        buttonLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_buttons);
        buttonLayout.removeAllComponents();

        buttons = new Button[buttonTexts.length];
        for (int i = 0; i < buttonTexts.length; i++) {
            buttons[i] = new Button(getContext());
            buttons[i].setText(buttonTexts[i]);
            buttons[i].setTextSize(buttonSize);
            buttons[i].setMarginsLeftAndRight(40, 40);
            int finalI = i;
            buttons[i].setClickedListener(component -> {
                buttonClick(finalI);
            });
            buttonLayout.addComponent(buttons[i]);
        }

        details = new DirectionalLayout[days];
        for (int i = 0; i < days; i++) {
            details[i] = new DirectionalLayout(getContext());
        }
    }

    private void showContactDetailDialog() {
        Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_select_year_month, null, false);

        DatePicker datePicker = (DatePicker) container.findComponentById(ResourceTable.Id_data_picker);
        Button button_yes = (Button) container.findComponentById(ResourceTable.Id_select_yes);
        Button button_no = (Button) container.findComponentById(ResourceTable.Id_select_no);

        CommonDialog commonDialog = new CommonDialog(this);
        commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
        commonDialog.setContentCustomComponent(container);
        commonDialog.show();

        button_yes.setClickedListener(component -> {
            show_year.setText(datePicker.getYear() + "年");
            show_month.setText(datePicker.getMonth() + "月");
            contactItemProvider.update(datePicker.getYear(), datePicker.getMonth());
            updateIncomeOutcome();
            commonDialog.hide();
        });

        button_no.setClickedListener(component -> {
            commonDialog.hide();
        });
    }

    private void buttonClick(int buttonNum) {
        if (buttonNum == 0) {
            present(new PropertySlice(), new Intent());
        } else if (buttonNum == 1) {
            present(new StatisticsSlice(), new Intent());
        } else if (buttonNum == 2) {
            present(new AddAccountSlice(), new Intent());
        } else if (buttonNum == 3) {
            present(new ImportBillSlice(), new Intent());
        }
    }


    private void initComponents() {
        initButtons();
        DirectionalLayout show_year_month = (DirectionalLayout) findComponentById(ResourceTable.Id_show_year_month);
        income = (Text) findComponentById(ResourceTable.Id_income);
        outcome = (Text) findComponentById(ResourceTable.Id_outcome);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_details);
        show_year = (Text) findComponentById(ResourceTable.Id_showYear);
        show_month = (Text) findComponentById(ResourceTable.Id_showMonth);
        Component btnAdd = findComponentById(ResourceTable.Id_bnt_add);
        btnAdd.setClickedListener(component -> {
            present(new AddAccountSlice(), new Intent());
        });
        show_year_month.setClickedListener(component -> {
            showContactDetailDialog();
        });

        Image image = (Image) findComponentById(ResourceTable.Id_arrow);
        PixelMap pixelMap = image.getPixelMap();
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = pixelMap.getImageInfo().pixelFormat;
        options.editable = true;
        options.size = new Size(70, 40);

        PixelMap pixelMapNew = PixelMap.create(pixelMap, options);
        image.setPixelMap(pixelMapNew);
    }


    private void initProvider() {
        contactItemProvider = new AccountItemProvider(this, new ArrayList<>());
        listContainer.setItemProvider(contactItemProvider);
        listContainer.setReboundEffect(true);
        contactItemProvider.update();
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
